package eddy.yang;
import java.awt.*;
import javax.swing.*;

public class TimerLabel extends JTextField implements Runnable {
   /**
    * 
    */
   private static final long serialVersionUID = 1L;
   private int seconds = 0;
   private volatile Thread timer;  
   
   public TimerLabel() {
      super(10);
      setEditable(false);
      Font f = new Font("SansSerif", Font.BOLD, 16);
      setFont(f);
      setForeground(Color.red);
      setText("" + seconds);
   }
   
   public void resetTime() {
      seconds = 0;
      setText("" + seconds);
      if(timer != null) {
         timer.stop();
         timer = null;
      }
   }
   
   public void start() {
		timer = new Thread(this);
      timer.start();
      seconds = 0;
      setText("" + seconds);
   }
   
   public void stop() {
        timer = null;
   }
    
   public void run() {
    
    Thread me = Thread.currentThread();
      while (timer == me) {
      	try {
      		Thread.sleep(1000);
      	} catch (InterruptedException e) {
      	}
      	seconds++;
      	setText("" + seconds);
      }
   }
}
